#pragma once
#include <fstream>
#include <iostream>
#include <cmath>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "../include/common.hpp"
#include "./track.cpp"

#define UP_LINE      (ROWSIMAGE/3)

// #define UP_LINE      (8)
#define DOWN_LINE    (ROWSIMAGE-16)

#define BREAK_BIAS   (COLSIMAGE/3)
class control
{
    private:
        int mid_line_list[ROWSIMAGE]={COLSIMAGE/2};
    public:
    //鍋忓樊鍊?
    int devation;

    void get_devation(track &track)
    {
        int bias=0;//鍙互鍒╃敤涓婁笅涓よ杈圭嚎鐨勫樊鍊兼潵鍒ゆ柇鏄惁鍑虹幇鏂甫
        int effective_count=0;
        devation=0;
        //鑾峰彇涓嚎
        //鑾峰彇鍋忓樊鍊?
        for (int i = 0; i < ROWSIMAGE; i++)
        {
            mid_line_list[i] = (track.l_border[i].y + track.r_border[i].y)/2;//姹備腑绾?
        }
        for (int i = DOWN_LINE; i > UP_LINE; i--)//姹傚亸宸€?
        {
            if(track.effective_row[i]==true)
            {
                effective_count++;
                devation =devation +(mid_line_list[i]-COLSIMAGE/2);
                if(abs(mid_line_list[i]-mid_line_list[i+1])>BREAK_BIAS)
                {break;}
        }
        }
        devation =devation/effective_count;
        return;
    }
    void draw_mid_line(Mat & binary_mat)
    {
        for (int i = 0; i < ROWSIMAGE; i++)
        {
            circle(binary_mat, Point(mid_line_list[i],i), 1,Scalar(0, 0, 255), -1); // 缁胯壊鐐?

        }
        putText(binary_mat, "devation:"+to_string(devation), Point(1, 30),FONT_HERSHEY_TRIPLEX, 0.3, Scalar(0, 0, 255), 1, CV_AA);
    }
};